﻿using Facebook.Client;
using Smile.Converter;
using System;
using System.Threading.Tasks;

namespace Smile.Network.OAuth
{
    public class FacebookClient : IOAuthClient
    {
        // @Porperties
        public string ConsumerKey
        {
            get
            {
                return _consumerKey;
            }
            set
            {
                _consumerKey = value;
            }
        }

        public string ConsumerSecret
        {

            get
            {
                return _consumerSecret;
            }
            set
            {
                _consumerSecret = value;
            }
        }

        public string Token
        {
            get
            {
                return _session.AccessToken;
            }
            set
            {
                _session.AccessToken = value;
            }
        }

        public string TokenSecret
        {
            get 
            {
                return "";
            }
        }

        public string Verifier
        {
            get 
            {
                return "";
            }
        }

        public string UserId
        {
            get
            {
                return _session.FacebookId;
            }
            set
            {
                _session.FacebookId = value;
            }
        }

        public DateTime Expires
        {
            get
            {
                return _session.Expires;
            }
        }

        public bool IsAuthenticated
        {
            get
            {
                return (DateTimeConverter.ToUnixTimeStamp(_session.Expires)
                    - DateTimeConverter.ToUnixTimeStamp(DateTime.Now) > 0);
            }
        }

        public async Task<bool> AuthenticateAsync(string consumerKey, string consumerSecret = "", string authorizeCallbackUrl = "")
        {
            ConsumerKey = consumerKey;

            if (IsAuthenticated)
            {
                return IsAuthenticated;
            }

            _facebookSessionClient = new FacebookSessionClient(ConsumerKey);
            if (string.IsNullOrWhiteSpace(Token))
            {

                // Authentication
                try
                {
                    _session = await _facebookSessionClient
                        .LoginAsync("user_about_me,read_stream");
                }
                catch (InvalidOperationException)
                {
                }
            }
            return true;
        }

        public Task<bool> AuthenticateAsync(string consumerKey, string authorizeCallbackUrl)
        {
            throw new NotImplementedException();
        }


        // @Private
        FacebookSessionClient   _facebookSessionClient;
        FacebookSession         _session = new FacebookSession();
        string                  _consumerKey = "";
        string                  _consumerSecret = "";
    }  
}
